package com.czz.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czz.manage.entity.Tag;
import com.czz.manage.mapper.TagMapper;
import com.czz.manage.query.TagQuery;
import com.czz.manage.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName: TagServiceImpl
 * @Author Czz
 * @Date 2020/2/18
 * @Time 18:12
 **/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    TagMapper tagMapper;

    @Override
    public void getTagPageList(Page<Tag> pageParam, TagQuery tagQuery) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");

        if (!ObjectUtils.isEmpty(tagQuery)){
            tagMapper.selectPage(pageParam,queryWrapper);
        }

        String name = tagQuery.getName();
        String begin = tagQuery.getBegin();
        String end = tagQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("tag_name",name);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        tagMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public Boolean deleteTagById(String id) {
        // 先判断有无存在该标签的问题
        Integer count = tagMapper.countTagQuestion(id);
        if (count > 0){
            return false;
        }
        tagMapper.deleteById(id);
        return true;
    }

    @Override
    public List<Tag> getAllTagList() {
        List<Tag> tagList = tagMapper.selectList(null);
        return tagList;
    }
}
